<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/11/9
 * Time: 14:15
 */

namespace Activity\Controller;
use Common\Controller\AdminbaseController;
use Activity\Model\ActivityModel;
use Order\Model\OrderModel;

class AdminactivityController extends AdminbaseController{

    protected $act_model;
    protected $page_size;

    function _initialize() {
        parent::_initialize();
        $this->page_size    = 20;
        $this->act_model    = M('activity');
    }


    // 活动列表
    public function lists()
    {
        $areas = getArea(2);
        $this->assign('area', json_encode($areas));

        $name = I('post.name');
        if (!empty($name)) {
            $where['name'] = array('like', "%$name%");
            $this->assign('name', $name);
        }

        $city_id = I('post.city_id', 0, 'intval');
        if (!empty($city_id)) {
            $where['city_id'] = $city_id;
            $this->assign('city_id', $city_id);
        }

        $start_time = I('post.start_time');
        if (!empty($start_time)) {
            $where['start_time'] = array('EGT', $start_time);
            $this->assign('start_time', $start_time);
        }

        $end_time   = I('post.end_time');
        if (!empty($end_time)) {
            $where['end_time'] = array('ELT', $end_time);
            $this->assign('end_time', $end_time);
        }

        $count  = $this->act_model->where($where)->count();
        $page   = $this->page($count, $this->page_size);
        $lists  = $this->act_model
            ->where($where)
            ->limit($page->firstRow , $page->listRows)
            ->order("sort desc, updated_at desc")
            ->select();

        $act_model = new ActivityModel();
        foreach ($lists as $k => $v) {
            if ($v['city_id']) {
                $city_info      = M('area')->where(['id' => $v['city_id']])->find();
                $v['city_name'] = $city_info['areaname'];
            }
            $v['act_form_name'] = $act_model->activity_form_array[$v['act_form']];

            // 订单数量
            $ticket_ids = M('act_ticket')->where(['act_id' => $v['id']])->field('id')->select();
            if ($ticket_ids) {
                $v['order_count'] = M('order')->where(['type' => OrderModel::ORDER_TYPE_ACT_TICKET, 'goods_id' => ['in', array_column($ticket_ids, 'id')]])->count();
            } else {
                $v['order_count'] = 0;
            }

            $lists[$k] = $v;
        }

        $this->assign("page", $page->show('Admin'));
        $this->assign("lists", $lists);
        $this->display();
    }


    // 新建，编辑 活动
    public function edit()
    {
        $id = I('get.id');
        if (IS_POST) {
            // 标签
            $labels                         = $_POST['info']['labels'];
            $labels                         = array_filter(explode(',', $labels));
            $_POST['info']['labels']        = implode(',', $labels);

            // 缩略图
            $thumb                          = array();
            $thumb['thumb']                 = sp_asset_relative_url($_POST['thumb']['thumb']);
            $thumb['pc_page_thumb']         = sp_asset_relative_url($_POST['thumb']['pc_page_thumb']);
            $thumb['mb_page_thumb']         = sp_asset_relative_url($_POST['thumb']['mb_page_thumb']);
            $_POST['info']['updated_at']    = time();

            // 城市
            $_POST['info']['city_id']       = $_POST['city_id'];
            $id                             = $id ?: $_POST['info']['id'];
            $data                           = I('post.info');
            $data['thumb']                  = json_encode($thumb);
            $data['content']                = htmlspecialchars_decode($data['content']);

            if (empty($data['refund_ticket_time'])) {
                unset($data['refund_ticket_time']);
            }
            if (empty($data['buy_ticket_time'])) {
                unset($data['buy_ticket_time']);
            }

            if ($id) {
                // 更新
                $this->act_model->where(['id' => $id])->save($data);
                $this->success('更新成功');
            } else {
                // 新建
                unset($data['id']);
                $data['created_at']     = time();
                $data['ticket_code']    = rand(100000, 999999);
                $result = $this->act_model->add($data);

                // 主动提交百度seo链接
                if (C('PUSH_SEO_URL')) {
                    $urls = array(C('HTTP_METHOD') . C('DOMAIN_NAME') . U('Activity/Activity/activity_info', ['id' => $result]));
                    post_baidu_seo_url($urls);
                }

                $this->success('添加成功');
            }
        } else {
            if (!empty($id)) {

                $act_info = $this->act_model
                    ->alias('a')
                    ->where(['id' => $id])
                    ->field('a.*, ( SELECT GROUP_CONCAT(name) FROM rx_label WHERE FIND_IN_SET(id, a.labels) ) as label_names')
                    ->find();

                if ($act_info) {

                    $del = I('del');
                    if ($del) {
                        $this->act_model->where(['id' => $id])->delete();
                        $this->success('操作成功');exit;
                    }

                    $act_info['label_names'] = array_filter(explode(',', $act_info['label_names']));
                    $act_model = new ActivityModel();
                    $act_info['act_form_name'] = $act_model->activity_form_array[$act_info['act_form']];
                    $act_info['thumb'] = json_decode($act_info['thumb'], true);
                    $this->assign('info', $act_info);
                }

                // 城市
                if ($act_info['city_id'] > 0) {
                    $path = getAreaParentPath($act_info['city_id'], getArea(2));
                    $path = $path ? $path : '';
                    $this->assign('path', json_encode($path));
                }
            }
            // 城市数据
            $areas = getArea(2);
            $this->assign('area', json_encode($areas));

            // 类型数据
            $act_model = new ActivityModel();
            $this->assign('form_types', $act_model->activity_form_array);

            // 模板数据
            $act_model = new ActivityModel();
            $this->assign('page_template', $act_model->activity_template_array);

            // 分组数据
            $group_lists = M('activity_group')->order('id desc')->limit(0)->select();
            $this->assign('group_lists', $group_lists);

            $this->display();
        }
    }

    // 活动订单列表
    public function order_lists()
    {
        $act_id         = I('id');
        $ticket_ids     = M('act_ticket')->where(['act_id' => $act_id])->field('id')->select();
        $ticket_ids     = array_filter(array_column($ticket_ids, 'id'));
        $where          = array();

        if (!empty($ticket_ids)) {

            // 支付状态
            $status = I('status');
            if ($status) {
                $this->assign('status', $status);
                $where['o.status'] = $status;
            }
//            else {
//                $where['o.status'] = ['in', [OrderModel::ORDER_STATUS_NOT_PAY, OrderModel::ORDER_STATUS_HAS_PAY]];
//            }
            // 退款状态
            $refund_status = I('refund_status');
            if ($refund_status) {
                $this->assign('refund_status', $refund_status);
                $where['o.refund_status'] = $refund_status;
            }

            $where['o.type']        = OrderModel::ORDER_TYPE_ACT_TICKET;
            $where['o.goods_id']    = ['in', $ticket_ids];

            $count          = M('order')->alias('o')->where($where)->count();
            $page           = $this->page($count, $this->page_size);
            $order_lists    = M('order')
                ->alias('o')
                ->join('__USERS__ u on u.id = o.user_id', 'left')
                ->join('__ACT_TICKET__ a on a.id = o.goods_id', 'left')
                ->where($where)
                ->limit($page->firstRow , $page->listRows)
                ->order("created_at desc")
                ->field('o.*, u.user_nicename, u.avatar, a.name, a.price')
                ->select();
            $this->assign("page", $page->show('default'));
            $this->assign("lists", $order_lists);
            $this->assign("act_id", $act_id);
        }

        $order_status = array(
            OrderModel::ORDER_STATUS_EXPIRE     => '已过期',
            OrderModel::ORDER_STATUS_NOT_PAY    => '未支付',
            OrderModel::ORDER_STATUS_HAS_PAY    => '已支付',
            OrderModel::ORDER_STATUS_REFUND     => '已退票'
        );
        $this->assign('order_status', $order_status);
        $this->display();
    }


    // 活动热门行家列表
    public function hot_guest_lists()
    {
        // 行家列表
        $pf_lists = M('professor')
            ->alias('p')
            ->join('__USERS__ u on u.id = p.user_id', 'left')
            ->field('p.activity_sort, p.activity, p.id, u.user_nicename, u.position, u.company, u.avatar')
            ->where(['p.status' => 1, 'p.del_time' => 0])
            ->order('p.activity_sort desc, p.pf_order desc')
            ->limit(0)
            ->select();

        $this->assign('pf_lists', $pf_lists);
        $this->display();
    }


    // 修改活动嘉宾列表
    public function edit_hot_guest_lists()
    {
        $id         = I('id');
        $op         = I('op'); // 操作

        if ($op == 'add') {
            $activity = 1;
        } elseif ($op == 'del') {
            $activity = 0;
        }
        M('professor')->where(['id' => $id])->save(['activity' => $activity]);
        $this->success('操作成功');
    }

    // 票种列表
    public function ticket_lists()
    {
        $act_id = I('act_id');
        if ($act_id) {
            $ticket_lists = M('act_ticket')->where(['act_id' => $act_id])->limit(0)->order('sort desc')->select();
            $this->assign('lists', $ticket_lists);
            $this->assign('act_id', $act_id);
            $this->display();
        } else {
            $this->error('数据有误');
        }
    }

    // 编辑票种
    public function ticket_edit()
    {
        if (IS_POST) {
            $id     = $_POST['info']['id'];
            $data   = I('post.info');

            if ($id) {
                // 更新
                $data['updated_at'] = time();
                M('act_ticket')->where(['id' => $id])->save($data);
                $this->success('更新成功');
            } else {
                // 新建
                unset($data['id']);
                $data['created_at'] = time();
                $data['remain_num'] = $data['total_num'];
                M('act_ticket')->add($data);
                $this->success('添加成功');
            }

        } else {
            $id     = I('get.id');
            $act_id = I('act_id');
            if (!empty($id)) {
                $ticket_info = M('act_ticket')
                    ->where(['id' => $id])
                    ->find();
                if ($ticket_info) {
                    $del = I('get.del');
                    if ($del) {
                        M('act_ticket')->where(['id' => $id])->delete();
                        $this->success('删除成功');exit;
                    }
                    $act_id     = $ticket_info['act_id'];
                    $act_info   = $this->act_model->where(['id' => $ticket_info['act_id']])->find();
                    if (strtotime($act_info['start_time']) < time()) {
                        $this->error('活动不可编辑');exit;
                    }
                }
                $this->assign('info', $ticket_info);
            }
            if ($act_id) {
                $this->assign('act_id', $act_id);
            }
            $this->display();
        }
    }


    // 活动邀请嘉宾列表
    public function guest_lists()
    {
        $act_id = I('get.act_id');
        if ($act_id) {
            $this->assign('act_id', $act_id);
            $lists = M('activity_guest')->where(['act_id' => $act_id])->order('sort desc')->limit(0)->select();
            $this->assign('lists', $lists);
            $this->display();
        }
    }

    //编辑活动邀请嘉宾
    public function edit_guest()
    {
        $act_id = I('get.act_id');
        $id     = I('get.id');
        if ($act_id) {
            $this->assign('act_id', $act_id);
            if (IS_POST) {
                $guest = I('post.guest');
                $guest['guest_img'] = sp_asset_relative_url($_POST['guest']['thumb']);

                $id = $id ?: $guest['id'];
                if ($id) {
                    M('activity_guest')->where(['id' => $id])->save($guest);
                } else {
                    unset($guest['id']);
                    M('activity_guest')->add($guest);
                }
                $this->success('操作成功');
            } else {
                if ($id) {
                    $del = I('get.del');
                    if ($del) {
                        M('activity_guest')->where(['id' => $id])->delete();
                        $this->success('删除成功');
                    }
                    $guest = M('activity_guest')->where(['id' => $id])->find();
                    $this->assign('guest', $guest);
                }
                $this->display();
            }
        }
    }

    // 活动步骤列表
    public function step_lists()
    {
        $act_id = I('get.act_id');
        if ($act_id) {
            $this->assign('act_id', $act_id);
            $lists = M('activity_step')->where(['act_id' => $act_id])->order('sort desc')->limit(0)->select();
            $this->assign('lists', $lists);
            $this->display();
        }
    }

    // 编辑活动步骤
    public function edit_step()
    {
        $act_id = I('get.act_id');
        $id     = I('get.id');
        if ($act_id) {
            $this->assign('act_id', $act_id);
            if (IS_POST) {
                $step = I('post.step');
                $step['step_guests'] = implode(',', $step['step_guests']);
                if (empty($step['step_guests'])) {
                    unset($step['step_guests']);
                }
                $id = $id ?: $step['id'];
                if ($id) {
                    M('activity_step')->where(['id' => $id])->save($step);
                } else {
                    unset($step['id']);
                    M('activity_step')->add($step);
                }
                $this->success('操作成功');
            } else {
                if ($id) {
                    $del = I('get.del');
                    if ($del) {
                        M('activity_step')->where(['id' => $id])->delete();
                        $this->success('删除成功');
                    }
                    $step = M('activity_step')->where(['id' => $id])->find();
                    $step['step_guests'] = explode(',', $step['step_guests']);
                    $this->assign('step', $step);
                }

                // 活动嘉宾
                $guest_lists = M('activity_guest')->where(['act_id' => $act_id])->order('sort desc')->limit(0)->select();
                $this->assign('guest_lists', $guest_lists);

                $this->display();
            }
        }
    }



    // 活动亮点列表
    public function bright_lists()
    {
        $act_id = I('get.act_id');
        if ($act_id) {
            $this->assign('act_id', $act_id);
            $lists = M('activity_bright')->where(['act_id' => $act_id])->order('sort desc')->limit(0)->select();
            $this->assign('lists', $lists);
            $this->display();
        }
    }

    // 编辑活动步骤
    public function edit_bright()
    {
        $act_id = I('get.act_id');
        $id     = I('get.id');
        if ($act_id) {
            $this->assign('act_id', $act_id);
            if (IS_POST) {
                $bright = I('post.bright');
                $bright['bright_img'] = sp_asset_relative_url($_POST['bright']['bright_img']);
                $id = $id ?: $bright['id'];
                if ($id) {
                    M('activity_bright')->where(['id' => $id])->save($bright);
                } else {
                    unset($bright['id']);
                    M('activity_bright')->add($bright);
                }
                $this->success('操作成功');
            } else {
                if ($id) {
                    $del = I('get.del');
                    if ($del) {
                        M('activity_bright')->where(['id' => $id])->delete();
                        $this->success('删除成功');
                    }
                    $bright = M('activity_bright')->where(['id' => $id])->find();
                    $this->assign('bright', $bright);
                }

                $this->display();
            }
        }
    }

    // 主/协办单位
    public function host_lists()
    {
        $act_id = I('get.act_id');
        if ($act_id) {
            $this->assign('act_id', $act_id);
            $lists = M('activity_host')->where(['act_id' => $act_id])->order('sort desc')->limit(0)->select();
            $this->assign('lists', $lists);
            $this->display();
        }
    }

    // 编辑主/协办单位
    public function edit_host()
    {
        $act_id = I('get.act_id');
        $id     = I('get.id');
        if ($act_id) {
            $this->assign('act_id', $act_id);
            if (IS_POST) {
                $host = I('post.host');
                $id = $id ?: $host['id'];
                if ($id) {
                    M('activity_host')->where(['id' => $id])->save($host);
                } else {
                    unset($host['id']);
                    M('activity_host')->add($host);
                }
                $this->success('操作成功');
            } else {
                if ($id) {
                    $del = I('get.del');
                    if ($del) {
                        M('activity_host')->where(['id' => $id])->delete();
                        $this->success('删除成功');
                    }
                    $host = M('activity_host')->where(['id' => $id])->find();
                    $this->assign('host', $host);
                }

                $this->display();
            }
        }
    }

    // 活动分组列表
    public function activity_group_lists()
    {
        $count = M('activity_group')->count();
        $page   = $this->page($count, $this->page_size);
        $lists  = M('activity_group')
            ->limit($page->firstRow , $page->listRows)
            ->order("id desc")
            ->select();

        $this->assign("page", $page->show('Admin'));
        $this->assign("lists", $lists);
        $this->display();
    }

    // 编辑活动分组
    public function edit_activity_group()
    {
        $id = I('get.id');
        if (IS_POST) {
            $group = I('post.group');
            $id = $id ?: $group['id'];

            if ($id) {
                M('activity_group')->where(['id' => $id])->save($group);
            } else {
                unset($group['id']);
                M('activity_group')->add($group);
            }
            $this->success('操作成功');

        } else {
            if ($id) {
                $del = I('get.del');
                if ($del) {
                    $has = M('activity')->where(['activity_group_id' => $id])->find();
                    if ($has) {
                        $this->error('该分类下还有关联的活动，不能删除');
                    } else {
                        M('activity_group')->where(['id' => $id])->delete();
                        $this->success('操作成功');
                    }
                } else {
                    $group = M('activity_group')->where(['id' => $id])->find();
                    $group['city_name_lists'] = array_filter(explode(',', $group['city_names']));
                    $this->assign('group', $group);
                }
            }

            // 城市数据
            $areas = getArea(2);
            $this->assign('area', json_encode($areas));
            $this->display();
        }
    }




    // 动态调整票种排序
    public function change_ticket_sort()
    {
        $id     = I('id', 0, 'intval');
        $order  = I('order', 0, 'intval');

        if ($id) {
            M('act_ticket')->where(['id' => $id])->save(['sort' => $order]);
            $this->success('修改成功');
        }
    }

    // 动态调整活动热门嘉宾排序
    public function change_pf_activity_sort()
    {
        $id     = I('id', 0, 'intval');
        $order  = I('order', 0, 'intval');

        if ($id) {
            M('professor')->where(['id' => $id])->save(['activity_sort' => $order]);
            $this->success('修改成功');
        }
    }

    // 动态调整活动排序
    public function change_acitvity_sort()
    {
        $id     = I('id', 0, 'intval');
        $order  = I('order', 0, 'intval');

        if ($id) {
            $this->act_model->where(['id' => $id])->save(['sort' => $order]);
            $this->success('修改成功');
        }
    }

    // 动态调整活动嘉宾排序
    public function change_acitvity_guest_sort()
    {
        $id     = I('id', 0, 'intval');
        $order  = I('order', 0, 'intval');

        if ($id) {
            M('activity_guest')->where(['id' => $id])->save(['sort' => $order]);
            $this->success('修改成功');
        }
    }

    // 动态调整活动步骤排序
    public function change_acitvity_step_sort()
    {
        $id     = I('id', 0, 'intval');
        $order  = I('order', 0, 'intval');

        if ($id) {
            M('activity_step')->where(['id' => $id])->save(['sort' => $order]);
            $this->success('修改成功');
        }
    }

    // 动态调整活动单位排序
    public function change_acitvity_host_sort()
    {
        $id     = I('id', 0, 'intval');
        $order  = I('order', 0, 'intval');

        if ($id) {
            M('activity_host')->where(['id' => $id])->save(['sort' => $order]);
            $this->success('修改成功');
        }
    }
}